﻿using Serilog;
using System.Reflection;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
var apiFilename = Assembly.GetExecutingAssembly().GetName().Name;
builder.Services.UseAppExtension(apiFilename);

// 开启jwt
var signKey = builder.Configuration.GetValue<string>("Authen:signKey");
var issuer = builder.Configuration.GetValue<string>("Authen:issuer");
var audience = builder.Configuration.GetValue<string>("Authen:audience");
builder.Services.UseJwtExtension(signKey, issuer, audience);

// 开始使用日志
var logSqlDns = builder.Configuration.GetConnectionString("logSqlDns");
builder.Services.UseSerilogExtension(logSqlDns, "log");
builder.Host.UseSerilog();

// 使用sql
var dbSqlDns = builder.Configuration.GetConnectionString("dbSqlDns");
builder.Services.UseSqlSugarExtension(SqlSugar.DbType.PostgreSQL, dbSqlDns, typeof(test),
    (sql) =>
    {
        Log.ForContext("level", "info")
        .ForContext("sql", sql)
        .Information("日常:sql执行记录" + sql);
    },
    (time, warnStr, parentWarnStr) =>
    {
        Log.ForContext("level", "warn")
        .ForContext("time", time)
        .ForContext("sql", warnStr)
        .ForContext("parentWarnStr", parentWarnStr)
        .Information("警告:sql执行记录" + warnStr);

    },
    (errorSql) =>
    {
        Log.ForContext("level", "error")
         .ForContext("sql", errorSql)
         .Information("异常:sql执行记录" + errorSql);
    }
);

// Casbin
builder.Services.UseCasbinExtion("CasbinConfigs", "rbac_model.conf", "rbac_policy.csv", dbSqlDns);


var app = builder.Build();

app.UseServiceLocator();
app.UseAppExtension(app.Environment);

// 授权和鉴权
app.UseAuthentication();
app.UseCasbinExtion();
app.UseAuthorization();

app.MapControllers();

app.Run();
